package com.itheima.easy.controller;
import com.itheima.easy.base.AjaxResult;
import com.itheima.easy.base.PageResponse;
import com.itheima.easy.dto.BillDto;
import com.itheima.easy.dto.PrepaidRechargeRecordDto;
import com.itheima.easy.service.BalanceService;
import com.itheima.easy.service.BillService;
import com.itheima.easy.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Bill控制器
 */
@Api(tags = "账单管理")
@RestController
@RequestMapping("/bill")
public class BillController {

    @Autowired
    private BillService billService;

    @Autowired
    private BalanceService balanceService;
    /**
     * 创建账单
     */
    @ApiOperation("创建账单")
    @PostMapping
    public AjaxResult create(@RequestBody BillDto bill) {
        billService.createMonthBill(bill);
        return AjaxResult.success();
    }

    /**
     * 支付账单
     */
    @ApiOperation("支付账单")
    @PutMapping
    public AjaxResult<TradingVo> pay(@RequestBody BillDto billDto) {
        TradingVo pay = billService.pay(billDto);
        return AjaxResult.success(pay);
    }

    /**
     * 创建账单线下支付记录
     */
    @ApiOperation("创建账单线下支付记录")
    @PostMapping("payRecord")
    public AjaxResult payRecord(@RequestBody TradingVo tradingVo) {
        this.billService.payRecord(tradingVo);
        return AjaxResult.success();
    }

    /**
     * 取消账单
     */
    @ApiOperation("取消账单")
    @PutMapping("cancel/{id}")
    public AjaxResult cancelById(@RequestBody BillDto billDto) {
        return AjaxResult.success(billService.cancelById(billDto));
    }

    /**
     * 根据id查询账单
     */
    @ApiOperation("根据id查询账单")
    @GetMapping("/{id}")
    public AjaxResult<BillVo> getById(@PathVariable Long id) {
        BillVo billVo = billService.selectByPrimaryKey(id);
        return AjaxResult.success(billVo);
    }

    /**
     * 分页查询账单
     */
    @ApiOperation("分页查询账单")
    @GetMapping("/page/")
    public AjaxResult<PageResponse<BillVo>> getBillPage(@ApiParam(value = "账单编号") @RequestParam(name = "billNo", required = false) String billNo,
                                                        @ApiParam(value = "老人姓名") @RequestParam(name = "elderName", required = false) String elderName,
                                                        @ApiParam(value = "老人身份证号") @RequestParam(name = "elderIdCard", required = false) String elderIdCard,
                                                        @ApiParam(value = "账单月份") @RequestParam(name = "billMonth", required = false) String billMonth,
                                                        @ApiParam(value = "支付状态") @RequestParam(name = "transactionStatus", required = false)  Integer transactionStatus,
                                                        @ApiParam(value = "页码（默认为1）") @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                                        @ApiParam(value = "每页数量（默认为10）") @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        PageResponse<BillVo> billPage = billService.getBillPage(billNo, elderName, elderIdCard, billMonth, transactionStatus, null, pageNum, pageSize);
        return AjaxResult.success(billPage);
    }

    /**
     * 分页查询欠费账单
     */
    @ApiOperation("分页查询欠费账单")
    @GetMapping("/arrears/")
    public AjaxResult<PageResponse<BillVo>> arrears(@ApiParam(value = "床位编号") @RequestParam(name = "bedNo", required = false) String bedNo,
                                                        @ApiParam(value = "老人姓名") @RequestParam(name = "elderName", required = false) String elderName,
                                                        @ApiParam(value = "页码（默认为1）") @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                                        @ApiParam(value = "每页数量（默认为10）") @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        PageResponse<BillVo> arrears = billService.arrears(bedNo, elderName, pageNum, pageSize);
        return AjaxResult.success(arrears);
    }

    /**
     * 分页查询余额
     */
    @ApiOperation("分页查询余额")
    @GetMapping("/balance/")
    public AjaxResult<PageResponse<BalanceVo>> balance(@ApiParam(value = "床位编号") @RequestParam(name = "bedNo", required = false) String bedNo,
                                                       @ApiParam(value = "老人姓名") @RequestParam(name = "elderName", required = false) String elderName,
                                                       @ApiParam(value = "页码（默认为1）") @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                                       @ApiParam(value = "每页数量（默认为10）") @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        PageResponse<BalanceVo> balanceVoPageResponse = balanceService.page(bedNo, elderName, pageNum, pageSize);
        return AjaxResult.success(balanceVoPageResponse);
    }

    /**
     * 创建预付费充值记录
     */
    @ApiOperation("创建预付费充值记录")
    @PostMapping("prepaidRechargeRecord")
    public AjaxResult createPrepaidRechargeRecord(@RequestBody PrepaidRechargeRecordDto dto) {
        billService.savePrepaidRechargeRecord(dto);
        return AjaxResult.success();
    }


    /**
     * 分页查询预付费充值记录
     */
    @ApiOperation("分页查询预付费充值记录")
    @GetMapping("/prepaidRechargeRecord/page")
    public AjaxResult<PageResponse<PrepaidRechargeRecordVo>> prepaidRechargeRecordPage(@ApiParam(value = "床位编号") @RequestParam(name = "bedNo", required = false) String bedNo,
                                                                                       @ApiParam(value = "老人姓名") @RequestParam(name = "elderName", required = false) String elderName,
                                                                                       @ApiParam(value = "页码（默认为1）") @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                                                                       @ApiParam(value = "每页数量（默认为10）") @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        PageResponse<PrepaidRechargeRecordVo> prepaidRechargeRecordVoPageResponse = billService.prepaidRechargeRecordPage(bedNo, elderName, pageNum, pageSize);
        return AjaxResult.success(prepaidRechargeRecordVoPageResponse);
    }

    /**
     * 分页查询资金流水
     */
    @ApiOperation("分页查询资金流水")
    @GetMapping("/fundFlow/page")
    public AjaxResult<PageResponse<FundFlowVo>> fundFlow(@ApiParam(value = "床位编号") @RequestParam(name = "bedNo", required = false) String bedNo,
                                                         @ApiParam(value = "老人姓名") @RequestParam(name = "elderName", required = false) String elderName,
                                                         @ApiParam(value = "资金方向") @RequestParam(name = "fundDirection", required = false) Integer fundDirection,
                                                         @ApiParam(value = "余额类型") @RequestParam(name = "balanceType", required = false) Integer balanceType,
                                                         @ApiParam(value = "页码（默认为1）") @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                                         @ApiParam(value = "每页数量（默认为10）") @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        PageResponse<FundFlowVo> fundFlow = billService.fundFlowPage(bedNo, elderName, fundDirection, balanceType, pageNum, pageSize);
        return AjaxResult.success(fundFlow);
    }
}
